import os

from service import Service

class BizService(Service):
    def __init__(self, year, week):
        self. __year = year
        self.__week = week

    def year(self):
        return self.__year
    def week(self):
        return self.__week

    def is_src_invalid(self, value):
        return not os.path.exists(value)        

    def is_result_invalid(self, value):
        dir_name = os.path.dirname(value)
        return not os.path.exists(dir_name)











class TradeLine(BizService):
    def __init__(self, year, week, trade):
        super().__init__(year, week)
        self.__trade = trade

    def run(self):
        src = self.__trade.get('src') 
        if src is None or self.is_src_invalid(src):
            return False

        result = self.__trade.get('result')
        if result is None or self.is_result_invalid(result):
            return False

        import trade_line_week 
        trade_line_week.trade_line_week(src, self.year(), self.week(), result)
        return True

class SalesRegion(BizService):
    def __init__(self, year, week, sales_region):
        super().__init__(year, week)
        self.__sales_region = sales_region

    def run(self):
        ib = self.__sales_region.get('ib')
        src = ib.get('src') 
        if src is None or self.is_src_invalid(src):
            return False

        result = ib.get('result')
        if result is None or self.is_result_invalid(result):
            return False

        import sales_region_week_ib
        sales_region_week_ib.sales_region_week_ib(src, self.year(), self.week(), result)


        ob = self.__sales_region.get('ob')
        src = ob.get('src') 
        if src is None or self.is_src_invalid(src):
            return False

        result = ob.get('result')
        if result is None or self.is_result_invalid(result):
            return False

        import sales_region_week_ob
        sales_region_week_ob.sales_region_week_ob(src, self.year(), self.week(), result)
        return True

class SalesCountry(BizService):
    def __init__(self, year, week, sales_country):
        super().__init__(year, week)
        self.__sales_country = sales_country

    def run(self):
        ib = self.__sales_country.get('ib')
        src = ib.get('src') 
        if src is None or self.is_src_invalid(src):
            return False

        result = ib.get('result')
        if result is None or self.is_result_invalid(result):
            return False

        import sales_country_week_ib
        sales_country_week_ib.sales_country_week_ib(src, self.year(), self.week(), result)


        ob = self.__sales_country.get('ob')
        src = ob.get('src') 
        if src is None or self.is_src_invalid(src):
            return False

        result = ob.get('result')
        if result is None or self.is_result_invalid(result):
            return False

        import sales_country_week_ob
        sales_country_week_ob.sales_country_week_ob(src, self.year(), self.week(), result)
        return True
